﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MVCGridGridViewTest.Controllers
{
    public class Person
    {
        public Person(int id, string name, Country country, int max, int petId, string petName, bool isMarried, DateTime birthDate)
        {
            Id = id;
            Name = name;
            Country = country;
            int r = new Random().Next(0, max);
            BirthDate = birthDate;
            PetId = petId;
            PetName = petName;
            IsMaried = isMarried;
        }

        public Person() { }

        public int Id { get; set; }
        public string Name { get; set; }
        public Country Country { get; set; }
        public DateTime BirthDate { get; set; }
        public int PetId { get; set; }
        public string PetName { get; set; }
        public bool IsMaried { get; set; }
    }

    public class Country : IEnumerable<string>, IComparable
    {
        public Country()
        {
        }
        public Country(string name, int id)
        {
            Name = name;
            Id = id;
        }
        public string Name { get; set; }
        public int Id { get; set; }


        public IEnumerator<string> GetEnumerator()
        {
            return null; ;
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            throw new NotImplementedException();
        }


        public int CompareTo(object obj)
        {
            return 1;
        }
    }
    public class ModelWrapper
    {
        public List<Person> personList { get; set; }
        public Person Person { get; set; }
        public List<Country> Countries { get; set; }
        public List<Pet> Pets { get; set; }
    }

    public class Pet
    {
        public Pet(string name, int id)
        {
            Name = name;
            Id = id;
        }
        public Pet()
        {
        }
        public string Name { get; set; }
        public int Id { get; set; }
    }

    public class homeController : Controller
    {
        public ActionResult Index(ModelWrapper model, string deleteid)
        {
            List<Country> countriesList = new List<Country>();
            countriesList.Add(new Country("Poland", 1));
            countriesList.Add(new Country("Germany", 2));
            countriesList.Add(new Country("Spain", 3));
            countriesList.Add(new Country("France", 4));
            countriesList.Add(new Country("Italy", 5));
            countriesList.Add(new Country("Norway", 6));
            countriesList.Add(new Country("Sweden", 7));

            List<Pet> petList = new List<Pet>();
            petList.Add(new Pet("Cat", 1));
            petList.Add(new Pet("Dog", 2));

            List<Person> personList = new List<Person>();
            personList.Add(new Person(1, "Tom", countriesList[1], 46, 2, "Dog", false, DateTime.Now.AddYears(-4)));
            personList.Add(new Person(2, "Kristof", countriesList[1], 456, 2, "Dog", false, DateTime.Now.AddYears(-24)));
            personList.Add(new Person(3, "Tymon", countriesList[1], 5, 2, "Dog", false, DateTime.Now.AddYears(-56)));
            personList.Add(new Person(4, "Ben", countriesList[0], 55, 1, "Cat", true, DateTime.Now.AddYears(-11)));
            personList.Add(new Person(5, "John", countriesList[1], 44, 2, "Dog", true, DateTime.Now.AddYears(-12)));
            personList.Add(new Person(6, "Mat", countriesList[1], 56, 2, "Dog", false, DateTime.Now.AddYears(-41)));
            personList.Add(new Person(7, "Stephen", countriesList[0], 55, 1, "Cat", true, DateTime.Now.AddYears(-34)));
            personList.Add(new Person(8, "Rebeca", countriesList[1], 565, 1, "Cat", false, DateTime.Now.AddYears(-43)));


            ModelWrapper modelWrapper = new ModelWrapper();
            modelWrapper.Countries = countriesList;
            modelWrapper.Pets = petList;
            modelWrapper.personList = personList; ;



            if (model.Person != null)
            {
                int ind = modelWrapper.personList.FindIndex(x => x.Id == model.Person.Id);
                modelWrapper.personList[ind] = model.Person;
                int countryId = modelWrapper.Countries.FindIndex(x => x.Id == model.Person.Country.Id);
                modelWrapper.personList[ind].Country = modelWrapper.Countries[countryId];
                modelWrapper.personList[ind].PetName = "Dog";
            }


            modelWrapper.Countries = countriesList;
            modelWrapper.personList.Remove(modelWrapper.personList.Find(c => c.Id.ToString() == deleteid));

            return View(modelWrapper);
        }

        //
        // GET: /home/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /home/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /home/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /home/Edit/5

        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /home/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /home/Delete/5

        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /home/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
    
   
